package cn.wolfcode.wolf2w.advice;

import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理  此除主要是获取后端逻辑处理异常
 * 然后将相应的异常信息反映到前端响应
 */
@ControllerAdvice
public class CommonExceptionHandler {
    //自定义逻辑异常
    @ExceptionHandler(LogicException.class)
    @ResponseBody
    public Object displayExp(LogicException e, Model model){
        e.printStackTrace();
        return JsonResult.error(JsonResult.CODE_ERROR, e.getMessage(), null);
    }
    //运行时异常
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object  runTimeExp(Exception e){
        e.printStackTrace();
        return JsonResult.defaultError();
    }
}
